<?php
/**
 *
 */
class Yz_Action_SelectById
    extends Yz_Action_Select
{

    /**
     *  @var int ?
     */
    protected $id;

    /** SQL rows limit, not required for SelectById / by primary keys
     *  @var bool(false)
     */
    protected $limit = false;



    /************************************
     *        Public methods         *
     ************************************/

    /**
     *  @param int id > 0
     *  @return this
     */
    public function setId( $id )
    {
        debug();
        assert('is_numeric($id) and $id > 0');
        $this->id = (int)$id;

        // prevent the action instance from returning results after changing parameters
        $this->count = null;
        return $this;
    }


    /************************************
     *        Protected methods         *
     ************************************/

    /** Verify arguments before building query
     *      The argument subject to verification is `id`
     *  @return this
     *  @throw StateError
     */
    protected function verifyArguments()
    {
        debug();
        if (! $this->id)
            throw StateError::_new_();
        return $this;
    }


    /**
     *  Build the common part of the SQL statement
     *  @abstract
     *  @return Yz_SQL_Statement
     */
    protected function buildStatement()
    {
        return parent::buildStatement()
            ->where(key($this->tables) . ".id=:_id")
        ;
    }

    /**
     *  Apply the arguments to the SQL statement
     *      Bind the id argument
     *  @param Yz_SQL_Statement $sql
     *  @return this
     */
    protected function applyArguments(Yz_SQL_Statement $sql)
    {
        debug();
        $sql
            ->bind("_id", $this->id)
        ;
        return $this;
    }


}
